package com.cdd.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.goods.mapper.GoodsBrandMapper;
import com.cdd.goods.mapper.GoodsSpecificationMapper;
import com.cdd.goods.service.IGoodsSpecificationService;
import com.common.entity.GoodsBrand;
import com.common.entity.GoodsSpecification;
import com.common.untils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lzl
 * @since 2022-09-01
 */
@Service
public class GoodsSpecificationServiceImpl implements IGoodsSpecificationService {
    @Autowired
    private GoodsSpecificationMapper goodsSpecificationMapper;
    @Override
    public CommonResult selectAll(GoodsSpecification goodsSpecification,Integer currentPage,Integer pageSize) {
        Page<GoodsSpecification> page=new Page<>(currentPage,pageSize);
        //System.out.println(goodsSpecification+"++++++");
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(goodsSpecification.getGsname())){
            wrapper.like("gsname",goodsSpecification.getGsname());
        }
        if (goodsSpecification.getIsDeleted()!=null){
            wrapper.eq("is_deleted",goodsSpecification.getIsDeleted());
        }
        //List list = goodsSpecificationMapper.selectList(wrapper);
        Page page1 = goodsSpecificationMapper.selectPage(page, wrapper);
        return new CommonResult(2000,"查询成功",page1);
    }

    @Override
    public CommonResult delectGoodsBrand(Integer id) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("id",id);
        GoodsSpecification goodsSpecification=new GoodsSpecification();
        goodsSpecification.setIsDeleted(1);
        goodsSpecificationMapper.update(goodsSpecification,wrapper);
        return new CommonResult(2000, "删除成功", null);
    }

    @Override
    public CommonResult updateGoodsBrand(GoodsSpecification goodsSpecification) {

      GoodsSpecification goodsSpecification1=new GoodsSpecification();
        goodsSpecification1.setId(goodsSpecification.getId());
        goodsSpecification1.setGsname(goodsSpecification.getGsname());
        goodsSpecificationMapper.updateById(goodsSpecification1);
        return new CommonResult(2000, "修改成功", null);
    }

    @Override
    public CommonResult insertGoods(GoodsSpecification goodsSpecification) {
//        GoodsSpecification goodsSpecification1=new GoodsSpecification();
//        goodsSpecification1.setGsname(goodsSpecification.getGsname());
        List<GoodsSpecification> goodsBrands = goodsSpecificationMapper.selectGsname();
        for (GoodsSpecification g:goodsBrands) {
            if (g.getGsname().equals(goodsSpecification.getGsname())){
                return new CommonResult(5000, "添加失败，已有该规格", null);
            }

        }
        goodsSpecification.setIsDeleted(0);
        goodsSpecificationMapper.insert(goodsSpecification);
        return new CommonResult(2000, "添加成功", null);
    }

    @Override
    public CommonResult select() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        List list = goodsSpecificationMapper.selectList(wrapper);
        return new CommonResult(2000, "查询成功", list);
    }
}
